﻿namespace MicroCloud.Utils
{
     /// <summary>
    /// 定义一个指定类型的单例，该实例的生命周期将跟随整个应用程序
    /// </summary>
    /// <typeparam name="T">要创建单例的类型</typeparam>
    public class Singleton<T> : Singleton
    {
        /// <summary>
        /// 获取指定类型的单例实例
        /// </summary>
        private static T instance;

        /// <summary>
        /// 获取或设置 指定类型的单例实例
        /// </summary>
        public static T Instance
        {
            get { return instance; }
            set
            {
                instance = value;
                AllSingletons[typeof(T)] = value;
            }
        }

    }

    /// <summary>
    /// 提供一个字典容器，按类型装载所有<see cref="Singleton{T}"/>的单例实例
    /// </summary>
    public class Singleton
    {
        /// <summary>
        /// 提供一个字典容器，按类型装载所有<see cref="Singleton{T}"/>的单例实例
        /// </summary>
        static Singleton()
        {
            AllSingletons ??= new Dictionary<Type, object>();
        }

        /// <summary>
        /// 获取 单例对象字典
        /// </summary>
        public static IDictionary<Type, object> AllSingletons { get; }

    }
}
